﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMSABL;
using WMSABL.SY;
using WMSAML.SY;
using ElectronicWMSA.Comm.ClassFiles;
using ElectronicWMSA.Comm.Forms.Message; 

namespace ElectronicWMSA.SY
{
    public partial class OrganizationRMFrm : Form
    {
        #region
        PublicDataSet bll_comm = new PublicDataSet();
        TOrganization_BLL bll_TOrganization = new TOrganization_BLL();
        public OrganizationRMFrm()
        {
            InitializeComponent(); 
            this.DBindOrganization();
        }
        #endregion

        #region 菜单绑定
        public void DBindOrganization()
        {
            this.treeViewDomain.Nodes.Clear();
            DataSet ds = new DataSet();
            ds = bll_TOrganization.GetList_TOrganization();
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            DataView dv = new DataView(dt);
            dv.RowFilter = "Parent_id=0";
            foreach (DataRowView drv in dv)
            {
                TreeNode node = new TreeNode();
                node.Text = drv["Organization_name"].ToString();
                node.Name = drv["Organization_id"].ToString();
                treeViewDomain.Nodes.Add(node);
                this.AddNodes(dt, node);
            }
            this.treeViewDomain.ExpandAll();
            this.treeViewDomain.SelectedNode = treeViewDomain.TopNode; //最顶端节点选中
        }

        /// <summary>
        /// AddNodes
        /// </summary>
        /// <param name="dt">dt</param>
        /// <param name="node">node</param>
        public void AddNodes(DataTable dt, TreeNode node)
        {
            DataView dv = new DataView(dt);
            dv.RowFilter = "Parent_id='" + node.Name + "'";
            foreach (DataRowView row in dv)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = row["Organization_name"].ToString();
                replyNode.Name = row["Organization_id"].ToString();
                node.Nodes.Add(replyNode);
                this.AddNodes(dt, replyNode);
            }
        }

        /// <summary>
        /// 展开
        /// </summary>
        private void labExpansion_Click(object sender, EventArgs e)
        {
            treeViewDomain.ExpandAll();
        }

        /// <summary>
        /// 折叠
        /// </summary>
        private void labFold_Click(object sender, EventArgs e)
        {
            treeViewDomain.CollapseAll();
        }
        #endregion

        #region 事件
        private int _Parent_id = 0;
        private int _id = 0;
        private void treeViewDomain_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            this._Parent_id = 0;
            if (e.Node != null)
            {
                TOrganization_Entity entity1 = new TOrganization_Entity();
                entity1 = bll_TOrganization.GetEntityByID(Convert.ToInt32(e.Node.Name));
                if (entity1 == null) return;
                TOrganization_Entity entity2 = new TOrganization_Entity();
                entity2 = bll_TOrganization.GetEntityByID(entity1.Parent_id);
                this._Parent_id = entity1.Organization_id;

                this.txtDomainAddHearID.Text = entity2.Organization_name;
                this.txtDomainAddCode.Text = entity1.Code;
                this.txtDomainAddName.Text = entity1.Organization_name;
                this.txtDomainAddEnglish.Text = entity1.English_Name;
                this.txtDomainAddNameShortening.Text = entity1.Short_Name;
                this.txtDomainAddMark.Text = entity1.Description;

                this.txtDomainAddHearID.ReadOnly = false;

                this.btnDomainAdd.Text = "修改";
            }
            this._id = Convert.ToInt32(e.Node.Name);
        }
        #endregion

        #region 新增/修改
        private void btnNew_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("新增")) return;
            if (_id > 0)
            {
                TOrganization_Entity entity1 = new TOrganization_Entity();
                entity1 = bll_TOrganization.GetEntityByID(_id);
                if (entity1 == null) return;
                TOrganization_Entity entity2 = new TOrganization_Entity();
                entity2 = bll_TOrganization.GetEntityByID(entity1.Parent_id);
                this._Parent_id = entity1.Organization_id;
                this.txtDomainAddHearID.Text = entity1.Organization_name;
                this.txtDomainAddCode.Text = "";
                this.txtDomainAddName.Text = "";
                this.txtDomainAddEnglish.Text = "";
                this.txtDomainAddNameShortening.Text = "";
                this.txtDomainAddMark.Text = "";
            }
            else
            {
                MessageClass.error("请选择目录......");
            }
        }

        private void btnDomainAdd_Click(object sender, EventArgs e)
        {
                Add();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateInfo();
        }

        private void Add()
        {
            if (!SystemVariable.VaildationPermission("新增")) return;

            TOrganization_Entity entity = new TOrganization_Entity();
            entity.Parent_id = _Parent_id;
            entity.Code = this.txtDomainAddCode.Text.Trim();
            entity.Organization_name = this.txtDomainAddName.Text.Trim();
            entity.English_Name = txtDomainAddEnglish.Text.Trim();
            entity.Short_Name = txtDomainAddNameShortening.Text.Trim();
            entity.Description = txtDomainAddMark.Text.Trim();
            entity.Creat_time = DateTime.Now;
            #region
            if (_Parent_id == 0)
            {
                MessageClass.error("请指定所属父级！");
                return;
            }
            if (this.txtDomainAddCode.Text.Trim() == "")
            {
                MessageClass.error("请填写代码！");
                this.txtDomainAddCode.Focus();
                return;
            }
            if (this.txtDomainAddName.Text.Trim() == "")
            {
                MessageClass.error("请填写名称！");
                this.txtDomainAddName.Focus();
                return;
            }
            #endregion
            if (MessageClass.enquire("确认添加信息！") == DialogResult.OK)
            {
                if (bll_TOrganization.Add(entity) > 0)
                    MessageClass.right("成功！");
                else
                   MessageClass.warning("失败！");

                this._id = 0;
                this.txtDomainAddHearID.Text = "";
                this.txtDomainAddCode.Text = "";
                this.txtDomainAddName.Text = "";
                this.txtDomainAddEnglish.Text = "";
                this.txtDomainAddNameShortening.Text = "";
                this.txtDomainAddMark.Text = "";
                this.DBindOrganization();
            }
        }

        private void UpdateInfo()
        {
            if (!SystemVariable.VaildationPermission("编辑")) return;

            TOrganization_Entity entity = new TOrganization_Entity();
            entity = bll_TOrganization.GetEntityByID(_Parent_id);
            entity.Parent_id = _Parent_id;
            entity.Code = this.txtDomainAddCode.Text.Trim();
            entity.Organization_name = this.txtDomainAddName.Text.Trim();
            entity.English_Name = txtDomainAddEnglish.Text.Trim();
            entity.Short_Name = txtDomainAddNameShortening.Text.Trim();
            entity.Description = txtDomainAddMark.Text.Trim();
            entity.Creat_time = DateTime.Now;
            #region
            if (_Parent_id == 0)
            {
                MessageClass.error("请指定所属父级！");
                return;
            }
            if (_Parent_id == 1)
            {
                MessageClass.error("根目录不可更改！");
                return;
            }
            if (this.txtDomainAddCode.Text.Trim() == "")
            {
                MessageClass.error("请填写代码！");
                this.txtDomainAddCode.Focus();
                return;
            }
            if (this.txtDomainAddName.Text.Trim() == "")
            {
                MessageClass.error("请填写名称！");
                this.txtDomainAddName.Focus();
                return;
            } 
            #endregion
            if (MessageClass.enquire("确认修改信息！") == DialogResult.OK)
            {
                if (bll_TOrganization.Update(entity))
                    MessageClass.right("成功");
                else
                    MessageClass.error("失败");

                this._id = 0;
                this.txtDomainAddHearID.Text = "";
                this.txtDomainAddCode.Text = "";
                this.txtDomainAddName.Text = "";
                this.txtDomainAddEnglish.Text = "";
                this.txtDomainAddNameShortening.Text = "";
                this.txtDomainAddMark.Text = "";
                this.DBindOrganization();
            }
        }
        #endregion

        #region 重置
        private void btnDomainClear_Click(object sender, EventArgs e)
        {
            this._id = 0;
            this.txtDomainAddHearID.Text = "";
            this.txtDomainAddCode.Text = "";
            this.txtDomainAddName.Text = "";
            this.txtDomainAddEnglish.Text = "";
            this.txtDomainAddNameShortening.Text = "";
            this.txtDomainAddMark.Text = "";
            this.DBindOrganization();
        }
        #endregion

        #region 删除
        TUsers_BLL bll_Tusers = new TUsers_BLL();
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("删除")) return;

            if (_id > 0)
            {
                if (bll_TOrganization.ExistsByPID(_id))
                {
                    MessageClass.error("资料已使用不能删除！");
                    return;
                }
                if (bll_Tusers.ExistsByOID(_id))
                {
                    MessageClass.error("资料已使用不能删除！");
                    return;
                }
                if (MessageClass.enquire("确定删除信息！") == DialogResult.OK)
                {
                    if (bll_TOrganization.Delete(_id))
                    {
                        MessageClass.right("成功！");
                        this._id = 0;
                        this.btnDomainClear_Click(sender, e);
                    }
                    else
                    {
                       MessageClass.error("失败！");
                    }
                }
            }
        }
        #endregion

        #region 退出
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion

      
    }
}
